package javaroke.servlets;

import java.io.IOException;

import javaroke.excepcion.DAOExcepcion;
import javaroke.modelo.Reserva;
import javaroke.modelo.Local;
import javaroke.modelo.Sala;
import javaroke.modelo.Cliente;
import javaroke.negocio.GestionReserva;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ReservaInsertarServlet
 */
@WebServlet("/ReservaInsertarServlet")
public class ReservaInsertarServlet extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet  {
	 static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReservaInsertarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	try {
		
		Reserva modelo = new Reserva();

		String fecha = request.getParameter("fecha");
		String descripcion = request.getParameter("descripcion");
		Integer sala = Integer.parseInt(request.getParameter("cboSala"));
		Integer cliente = Integer.parseInt(request.getParameter("txtCliente"));
		Integer local = Integer.parseInt(request.getParameter("cboLocal"));

		//instancias
		Sala sal = new Sala();
		Cliente cli = new Cliente();
		Local loc = new Local();
		
		modelo.setFecha(fecha);
		modelo.setDescripcion(descripcion);
		modelo.setElog(0);
		
		sal.setIdsala(sala);
		modelo.setSala(sal);
		
		cli.setIdcliente(cliente);
		modelo.setCliente(cli);
		
		loc.setIdlocal(local);
		modelo.setLocal(loc);
		
		GestionReserva negocio = new GestionReserva();
		try {
			negocio.inserta(modelo);
			request.setAttribute("MENSAJE", "Se registro correctamente.");

		    } catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}		
	//--
	} catch (IllegalArgumentException e2) {
		// TODO Auto-generated catch block
		e2.printStackTrace();
		request.setAttribute("MENSAJE", "ERROR, Falta llenar campos o tipo de variable no soportada: -" + e2.getMessage() + "-");
}
	
		RequestDispatcher rd = request
				.getRequestDispatcher("BuscarReserva.jsp");
		rd.forward(request, response);
	
			
	}
	
}





